# find numerical differentiation for a function
"""This module finds the error between numerically differentiating a function and exact value. Some simple functions have been tested.
"""
def diff(f, x, h = 1e-6):
	"""
	Define the numerical differentiation.

	f: the function that needs to be differentiated
	x: variable, will be given later
	h: a small number by default 1e-6
 
	Returns:  differentiation value
	"""
	value = (f(x+h)-f(x-h))/(2.*h)
	return value

from math import exp, cos, log, pi
def f1(x):
	return exp(x)
def f2(x):
	return exp(-2*x**2)
def f3(x):
	return cos(x)
def f4(x):
	return log(x)

# exact differentiation function
# e1 = exp(x)
# e2 = -4*x*exp(-2*x**2)
# e3 = -sin(x)
# e4 = 1./x

error1 = abs(diff(f1, 0, 0.01) - exp(0))
error2 = abs(diff(f2, 0, 0.01) - 0)
error3 = abs(diff(f3, 2*pi, 0.01) - 0)
error4 = abs(diff(f4, 1, 0.01) - 1)
print error1, error2, error3, error4,
